import { _pick, isUndefined } from "mmb";
import { AppCTX } from "../../types/koaExtend";
import { createBrandApiMWRouter } from "../../middleware/brand-api/brandAdminChecker";
import pageMW from "../../middleware/pager";
import {
  listBrandDistributeRequestWithUserInfosByBrandID,
  auditBrandDistributeRequest,
} from "../../service/brandDistributeRequestService";
import AuditResult, { isAuditResult } from "../../bg-enums/auditResult";
const router = createBrandApiMWRouter();

// 获取品牌分销员申请列表
router.get(
  "brand-distributeRequests",
  "/brd/:brandId/distributeRequests",
  pageMW,
  async (ctx: AppCTX, next) => {
    const { brandId } = ctx.params;
    const { userId, limit, offset, ts } = ctx;
    let isWaitAudit =
      isUndefined(ctx.query.isWaitAudit) || ctx.query.isWaitAudit === "true";

    const { total, items } =
      await listBrandDistributeRequestWithUserInfosByBrandID(
        brandId,
        isWaitAudit,
        {
          limit,
          offset,
          ts,
        }
      );

    ctx.body = {
      total,
      rows: items.map((beq) => ({
        ..._pick(
          beq,
          "brandDistributeRequestId",
          "brandId",
          "userId",
          "requested",
          "applyFields",
          "auditResult",
          "userPic"
        ),
      })),
    };
  }
);

// // 移除品牌分销员申请
// router.delete("/brd/:brandId/distributeRequest/:distributeRequestId", async (ctx: AppCTX, next) => {
//   const { brandId, distributeRequestId } = ctx.params;
//   const { userId } = ctx;
//   //2 需要发送异步消息给分销员申请系统
//   ctx.body = {
//     brandId,
//     userId, //操作人
//     distributeRequestId,
//   };
// });

// 审核品牌分销员申请
const audit = async (
  brandId: string,
  brandDistributeRequestId: string,
  auditResult: AuditResult
) => {
  const brandDistributeRequest = await auditBrandDistributeRequest({
    brandDistributeRequestId,
    audited: Date.now(),
    auditResult,
  });

  //2 根据sync_id发送异步消息给分销员申请系统
  return {
    ..._pick(
      brandDistributeRequest,
      "brandId",
      "userId",
      "userId",
      "requested",
      "applyFields",
      "audited",
      "auditResult"
    ),
  };
};

router.post(
  "audit-brandDistributeRequest",
  "/brd/:brandId/distributeRequest/:brandDistributeRequestId/audit/:auditResult",
  async (ctx: AppCTX, next) => {
    const { brandId, brandDistributeRequestId, auditResult } = ctx.params;
    const { userId } = ctx;

    if (!isAuditResult(auditResult)) {
      ctx.throw(400, "auditResult is invalid");
    } else {
      if (auditResult === AuditResult.PENDING) {
        ctx.throw(400, "auditResult is invalid2");
      }
    }
  }
);

// 审核通过分销员申请
router.post(
  "/brd/:brandId/distributeRequest/:brandDistributeRequestId/approve",
  async (ctx: AppCTX, next) => {
    const { brandId, brandDistributeRequestId } = ctx.params;
    ctx.body = await audit(
      brandId,
      brandDistributeRequestId,
      AuditResult.APPROVED
    );
    // const url = router.url("audit-brandDistributeRequest", { brandId, brandDistributeRequestId, auditResult: AuditResult.APPROVED });
    // ctx.redirect(url);
    // ctx.status = 301;
  }
);

// 审核拒绝分销员申请
router.post(
  "/brd/:brandId/distributeRequest/:brandDistributeRequestId/reject",
  async (ctx: AppCTX, next) => {
    const { brandId, brandDistributeRequestId } = ctx.params;
    ctx.body = await audit(
      brandId,
      brandDistributeRequestId,
      AuditResult.REJECTED
    );

    // const url = router.url("audit-brandDistributeRequest", { brandId, brandDistributeRequestId, auditResult: AuditResult.REJECTED });
    // ctx.redirect(url);
    // ctx.status = 301;
  }
);

export default router;
